package com.xgst.land.utils

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ThreadUtils
import com.xgst.land.App
import com.xgst.land.R
import java.util.Timer
import java.util.TimerTask
import kotlin.concurrent.schedule

/**
 *创建人：东
 *时间：2021/1/28
 */
@SuppressLint("MissingInflatedId")
object ProgressUtils{

    private var toast:Toast? = null
    private var task: TimerTask ? = null
    private val view:View
    private val tvProgress:TextView

    init {
        view = LayoutInflater.from(App.context).inflate(R.layout.progress_layout,null)
        tvProgress = view.findViewById(R.id.tvProgress)
    }

    fun showProgress(isLoop:Boolean = false){
        runOnMain {
            show()
            task?.cancel()
            task = Timer().schedule(3000){
                if(isLoop){
                    showProgress()
                }else{
                    runOnMain { show("请稍后，加载中...") }
                }
            }
        }
    }

   private fun show(content:String = "加载中..."){
        task?.cancel()
        toast?.cancel()
        toast = Toast(App.context)
        toast!!.let {
            it.setGravity(Gravity.CENTER,0,0)
            it.duration = Toast.LENGTH_LONG
            it.view = view
            tvProgress.text = content
            it.show()
        }
    }

    fun dismiss(){
        runOnMain {
            task?.cancel()
            toast?.cancel()
        }
    }
}

